//
//  ComposeViewController.swift
//  我的微博
//
//  Created by 李献红 on 15/10/27.
//  Copyright © 2015年 cn.lixianhong. All rights reserved.
//

import UIKit

class ComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor.whiteColor()

        
        prepareNav()
        prepareToolBar()
    }
    
    /// 准备导航栏
    private func prepareNav() {
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        navigationItem.rightBarButtonItem?.enabled = false
        
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 32))
        let titleLable = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        titleView.addSubview(titleLable)
        titleLable.text = "发微博"
        titleLable.sizeToFit()
//        titleLable.ff_AlignInner(type: ff_AlignType.TopCenter, referView: titleView, size: nil)
        titleLable.translatesAutoresizingMaskIntoConstraints = false
        titleView.addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        titleView.addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
    
        
        let nameLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 13)
        nameLabel.text = UserAccount.sharedAccount?.name ?? ""
        nameLabel.sizeToFit()
        titleView.addSubview(nameLabel)
//        nameLabel.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: titleView, size: nil)
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        titleView.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        titleView.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        navigationItem.titleView = titleView
        
    
    }
    
    /// 工具栏
    private lazy var toolBar: UIToolbar = UIToolbar()
    
    /// 添加工具栏
    private func prepareToolBar() {
    
        //1.添加工具栏
        view.addSubview(toolBar)
        toolBar.backgroundColor = UIColor.redColor()
        toolBar.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: view, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44))
        
        //2.准备按钮的数组
        let itemSettings = [["imageName": "compose_toolbar_picture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]

        
        //3.遍历数组按钮添加按钮
        var items = [UIBarButtonItem]()
        for item in itemSettings {
            let button = UIButton()
            button.setImage(UIImage(named: item["imageName"]!), forState: UIControlState.Normal)
            button.setImage(UIImage(named: item["imageName"]! + "_highlighted"), forState: UIControlState.Highlighted)
            button.sizeToFit()
            items.append(UIBarButtonItem(customView: button))
            
            //添加弹簧
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
            
        }
        items.removeLast()
        toolBar.items = items
    }
    
    
    
    func close() {
    
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func sendStatus() {
    
        print(__FUNCTION__)
    }
    


}
